package LeetCode.month01.day28;

import java.util.*;

public class Code01 {
    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        PriorityQueue<Map.Entry<String,Integer>> max = new PriorityQueue<>((a,b)->{
            if(a.getValue().equals(b.getValue())){
                return a.getKey().compareTo(b.getKey());
            }
            return b.getValue()-a.getValue();
        });
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s = sc.next();
            if(s.length()<=2){
                boolean flag = true;
                for (int i = 0; i < s.length(); i++) {
                    if(!Character.isDigit(s.charAt(i))){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    int num = Integer.parseInt(s);
                    max.addAll(map.entrySet());
                    while(num>0){
                        System.out.println(max.poll().getKey());
                        num--;
                    }
                    max.clear();
                }else{
                    map.put(s,map.getOrDefault(s,0)+1);
                }
            }else{
                map.put(s,map.getOrDefault(s,0)+1);
            }
        }
    }
}
